<?php

	$files_array = files_array( _DOCUMENT_ROOT, true, true); //myPrint( reset($files_array), '$files_array');
	
	function createFilesTree( array $files_array ){
		$echo='';
		$extPAGE = array('htm', 'html','php');
		$extTXT = array('txt', 'htm', 'html', 'css','js','php','xml', 'log');
		$extIMG = array('bmp', 'jpg', 'jpeg', 'png','gif','ico');
		
		//$onclick = "onclick='var sd=this.parentNode.lastChild.style.display; this.parentNode.lastChild.style.display=sd==\"\"?\"none\":\"\"'";
		$onclick = 'onclick="toggleDir( this )"';
		//sorting: directories + files
		$dirs = array(); $files= array(); 
		foreach($files_array as $path=>$val){
			if(is_array($val)) $dirs[$path]=$val;
			else $files[$path]=$val;
		}
		$_files_array=$dirs+$files;
		 
		foreach($_files_array as $path=>$val){
			/* относительный путь */
			$relPath = str_replace( _DOCUMENT_ROOT, '', $path) ;
			$onclickWO = 'onclick="window.open( this.href); return false;"';//, \'admin_popur_edit\', \'width=600,height=800\'
			$prop_but = '<a '.$onclickWO.' title="'.Lang::get('TO_ADJUST').'..." class="setting_but" href="/admin/modules/content/PageProperty.php?page_path='.$relPath.'"></a>';
			$TextFileEdit = '/admin/modules/content/TextFileEdit.php';
			$edit_but = '<a '.$onclickWO.' title="'.Lang::get('TO_EDIT').'..." class="edit_but" href="'.$TextFileEdit.'?file_path='.$relPath.'"></a>'; if( !checkAccess( array('1') ) ) $edit_but = '';
			$download_but = '<a title="'.Lang::get('TO_DOWNLOAD').'..." class="download_but" href="'.ADMIN_DOWNLOAD_MODULE.'?download_file_path='.$relPath.'"></a>';
			$add_file = '<a '.$onclickWO.' title="'.Lang::get('TO_CREATE').'..." class="file_add" href="'.$TextFileEdit.'?file_path='.$relPath.'/new_file.html"></a>';
			$edit_chb = '<input type="checkbox" name="edit_file_path[]" value="'.$relPath.'">';
			
			if(is_array($val)){
				$echo.= '<li>'.
					//$prop_but.
					'<span class="dir" id="'.$relPath.'" '.(count($val)>0 ?$onclick :'').'" >'.// id="'.$relPath.'
					basename( $path ).' ( '.(count($val)).' )</span>'.$add_file;//
					if(count($val)>0){
						$echo.= '<ol class="file_tree" style="display:none;">';
							$echo.=createFilesTree( $val );
						$echo.='</ol>';
					}
				$echo.= '</li>';
			}else{
				$path_info = pathinfo($val);
				$fclass = isset($path_info['extension'])? $path_info['extension'] : 'file' ;
				if(in_array($fclass, $extIMG)){
					$relPath = iconv("cp1251", "utf-8", $relPath );
					$isize=array(0=>600,1=>800);
					if(!preg_match('/[А-я]/', $path)) $isize = getimagesize($path);
					$onclickWOI = 'onclick="var imW=window.open( this.href, \'admin_popur_image\', \'width='.($isize[0]+70).',height='.($isize[1]+70).'\'); return false;"';
					$val = '<a '.$onclickWOI.' href="../show_image.php?src='.$relPath.'">'.$val.'</a>';
				}
					
				$echo.= '<li class="file_tree">'.
					//$edit_chb.
					$download_but.( in_array($fclass, $extTXT )? $edit_but :'' ).
					'<span class="file '.$fclass.'">'.$val.'</span>'.
				'</li>';
			}
		}
		return $echo;
	}
	
	$filesTree = createFilesTree( reset($files_array) );
	
	$outForm = '<form method="post" class="panel inline-block" name="table_edit_form" action="">'.
			'<ul class="file_tree">'.
				$filesTree.
				'<li class="file_tree">'.
					'<a '.'onclick="window.open( this.href); return false;"'.//, \'admin_popur_edit\', \'width=600,height=800\'
					' title="'.Lang::get('TO_CREATE').'..." class="file_add" href="'.'/admin/modules/content/TextFileEdit.php'.'?file_path=/new_file.html"></a>'.
				'</li>'.
			'</ul>'.
			//'<input type="checkbox" name="check_all" value="edit_row_id[]">'.
			//'Отмеченные: '.
			//'<input type="submit" class="confirm_submit" name="delete_files" value="'.Lang::get('TO_DELETE').'">'.
		'</form>';
	
	echo $outForm;
?>
<script type="text/javascript">
//checkFrame();/*загрузка только во врейме*/
function openDir( id ){
	var elem = document.getElementById( id );
	if(!elem) return;

	var	parent=elem.parentNode;
	elem.style.display = ''; parent.lastChild.style.display = '';
	addClass( elem, 'active');
	while( parent && parent.name!="table_edit_form"){
		parent.style.display = '';
		parent = parent.parentNode;
	}
	document.location.href=document.location.href.split('#')[0]+'#'+id;
}

function toggleDir( elem ){
	var sd=elem.parentNode.lastChild.style.display; 
	if(sd!='none'){
		elem.parentNode.lastChild.style.display='none';
		removeClass( elem, 'active');
	}
	else openDir( elem.id );
		
}

/* once on load */
openDir( document.location.href.split('#').pop() );
</script>